import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as session from 'express-session';
// 异常拦截器
import { ExceptionInterceptor } from './interceptor/ExceptionInterceptor';
// 注册全局 DTO 验证管道
import { VerifyPipe } from './pipe/verify/verify.pipe';
// 全局响应拦截器
import { ResIntercept } from './interceptor/ResIntercept';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  const PORT = Number(process.env.PORT);
  app.useGlobalFilters(new ExceptionInterceptor());
  app.useGlobalInterceptors(new ResIntercept());
  app.useGlobalPipes(new VerifyPipe());
  app.useStaticAssets(
    process.env.STORAGE_ADDRESS || join(__dirname, '../fileStorage'),
    {
      prefix: process.env.PREFIX,
    },
  );
  app.use(
    session({
      secret: 'reindeer',
      name: 'reindeer.session',
      rolling: true,
    }),
  );
  app.enableCors(); // 👈 允许所有跨域请求
  await app.listen(PORT);
}
bootstrap();
